<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="80px">
            <el-form-item label="所属电站" prop="pvId">
                <el-select v-model="queryParams.pvId" filterable placeholder="请选择所属电站" style="width: 100%;"
                    @keyup.enter.native="handleQuery">
                    <el-option v-for="(item, index) in pvStationList" :key="item.id" :label="item.name"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="设备名称" prop="deviceName">
                <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="设备SN" prop="deviceSn">
                <el-input v-model="queryParams.deviceSn" placeholder="请输入设备SN" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="设备PN号" prop="devicePn">
                <el-input v-model="queryParams.devicePn" placeholder="请输入设备PN号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="设备编码" prop="deviceCode">
                <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['business:inverter:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['business:inverter:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['business:inverter:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['business:inverter:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="inverterList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" fixed="left" />
            <!-- <el-table-column label="光伏电站设备ID" align="center" prop="deviceId" /> -->
            <el-table-column label="所属电站" align="center" prop="pvId" fixed="left">
                <template slot-scope="scope">
                    {{ scope.row.params.pvName }}
                </template>
            </el-table-column>
            <el-table-column label="设备名称" align="center" prop="deviceName" fixed="left" />
            <el-table-column label="设备SN" align="center" prop="deviceSn" width="180" show-overflow-tooltip />
            <el-table-column label="设备PN号" align="center" prop="devicePn" width="180" show-overflow-tooltip />
            <el-table-column label="设备厂商" align="center" prop="manufacturer">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.pv_station_equipment_manufacturer" :value="scope.row.manufacturer" />
                </template>
            </el-table-column>
            <el-table-column label="设备编码" align="center" prop="deviceCode" width="120" show-overflow-tooltip />
            <el-table-column label="设备型号" align="center" prop="deviceModel" width="120" show-overflow-tooltip />
            <el-table-column label="设备类型" align="center" prop="deviceType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.pv_station_device_type" :value="scope.row.deviceType" />
                </template>
            </el-table-column>
            <el-table-column label="协议版本" align="center" prop="protocolVersion" width="100" />
            <el-table-column label="实时功率" align="center" prop="realtimePower" width="100">
                <template slot-scope="scope">
                    {{ scope.row.realtimePower }} KW
                </template>
            </el-table-column>
            <el-table-column label="额定功率" align="center" prop="ratedPower">
                <template slot-scope="scope">
                    {{ scope.row.ratedPower }} KW
                </template>
            </el-table-column>
            <el-table-column label="额定电压" align="center" prop="ratedVoltage">
                <template slot-scope="scope">
                    {{ scope.row.ratedVoltage }} V
                </template>
            </el-table-column>
            <el-table-column label="接入容量" align="center" prop="accessCapacity">
                <template slot-scope="scope">
                    {{ scope.row.accessCapacity }} KW
                </template>
            </el-table-column>
            <el-table-column label="满发小时" align="center" prop="fullLoadHours">
                <template slot-scope="scope">
                    {{ scope.row.fullLoadHours }} h
                </template>
            </el-table-column>
            <el-table-column label="当日发电量时" align="center" prop="dayPowerGeneration" width="100">
                <template slot-scope="scope">
                    {{ scope.row.dayPowerGeneration }} KWh
                </template>
            </el-table-column>
            <el-table-column label="当月发电量" align="center" prop="monthPowerGeneration" width="100">
                <template slot-scope="scope">
                    {{ scope.row.monthPowerGeneration }} KWh
                </template>
            </el-table-column>
            <el-table-column label="总年发电量" align="center" prop="yearPowerGeneration" width="100">
                <template slot-scope="scope">
                    {{ scope.row.yearPowerGeneration }} KWh
                </template>
            </el-table-column>
            <el-table-column label="累计发电量" align="center" prop="totalPowerGeneration" width="100">
                <template slot-scope="scope">
                    {{ scope.row.totalPowerGeneration }} KWh
                </template>
            </el-table-column>
            <el-table-column label="累计运行时间" align="center" prop="totalRuntime" width="100">
                <template slot-scope="scope">
                    {{ scope.row.totalRuntime }} h
                </template>
            </el-table-column>
            <el-table-column label="软件版本DC-AC" align="center" prop="dcacSoftwareVersion" width="120" />
            <el-table-column label="软件版本DC-DC" align="center" prop="dcdcSoftwareVersion" width="120" />
            <el-table-column label="参数版本DC-AC" align="center" prop="dcacParamVersion" width="120" />
            <el-table-column label="参数版本DC-DC" align="center" prop="dcdcParamVersion" width="120" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:inverter:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:inverter:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改光伏电站逆变器设备信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="所属电站" prop="pvId">
                            <el-select v-model="form.pvId" placeholder="请选择所属电站" filterable style="width: 100%;">
                                <el-option v-for="(item, index) in pvStationList" :key="item.id" :label="item.name"
                                    :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备名称" prop="deviceName">
                            <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备SN" prop="deviceSn">
                            <el-input v-model="form.deviceSn" placeholder="请输入设备SN" maxlength="20" show-word-limit />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备PN号" prop="devicePn">
                            <el-input v-model="form.devicePn" placeholder="请输入设备PN号" maxlength="20" show-word-limit />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备厂商" prop="manufacturer">
                            <el-select v-model="form.manufacturer" placeholder="请选择设备厂商" filterable
                                style="width: 100%;">
                                <el-option v-for="dict in dict.type.pv_station_equipment_manufacturer" :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备编码" prop="deviceCode">
                            <el-input v-model="form.deviceCode" placeholder="请输入设备编码" maxlength="8" show-word-limit />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备型号" prop="deviceModel">
                            <el-input v-model="form.deviceModel" placeholder="请输入设备型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备类型" prop="deviceType">
                            <el-select v-model="form.deviceType" placeholder="请选择设备类型" filterable style="width: 100%;">
                                <el-option v-for="dict in dict.type.pv_station_device_type" :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="协议版本" prop="protocolVersion">
                            <el-input v-model="form.protocolVersion" placeholder="请输入协议版本" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="实时功率" prop="realtimePower">
                            <el-input-number :precision="2" v-model="form.realtimePower" placeholder="请输入实时功率，单位kW 千瓦"
                                style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="额定功率" prop="ratedPower">
                            <el-input-number :precision="2" v-model="form.ratedPower" placeholder="请输入额定功率，单位kW 千瓦"
                                style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="额定电压" prop="ratedVoltage">
                            <el-input-number :precision="2" v-model="form.ratedVoltage" placeholder="请输入额定电压，单位V 伏"
                                style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="接入容量" prop="accessCapacity">
                            <el-input-number :precision="2" v-model="form.accessCapacity" placeholder="请输入接入容量，单位kW 千瓦"
                                style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="满发小时" prop="fullLoadHours">
                            <el-input-number :precision="2" v-model="form.fullLoadHours" placeholder="请输入满发小时，单位h 小时"
                                style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="12">
                        <el-form-item label="当日发电量" prop="dayPowerGeneration">
                            <el-input v-model="form.dayPowerGeneration" placeholder="请输入当日发电量，单位kWh 千瓦时" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="12">
                        <el-form-item label="软件版本DC-AC" prop="dcacSoftwareVersion">
                            <el-input v-model="form.dcacSoftwareVersion" placeholder="请输入软件版本DC-AC" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="软件版本DC-DC" prop="dcdcSoftwareVersion">
                            <el-input v-model="form.dcdcSoftwareVersion" placeholder="请输入软件版本DC-DC" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="参数版本DC-AC" prop="dcacParamVersion">
                            <el-input v-model="form.dcacParamVersion" placeholder="请输入参数版本DC-AC" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="参数版本DC-DC" prop="dcdcParamVersion">
                            <el-input v-model="form.dcdcParamVersion" placeholder="请输入参数版本DC-DC" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- <el-form-item label="当月发电量，单位kWh 千瓦时" prop="monthPowerGeneration">
                    <el-input v-model="form.monthPowerGeneration" placeholder="请输入当月发电量，单位kWh 千瓦时" />
                </el-form-item>
                <el-form-item label="总年发电量，单位kWh 千瓦时" prop="yearPowerGeneration">
                    <el-input v-model="form.yearPowerGeneration" placeholder="请输入总年发电量，单位kWh 千瓦时" />
                </el-form-item>
                <el-form-item label="累计发电量，单位kWh 千瓦时" prop="totalPowerGeneration">
                    <el-input v-model="form.totalPowerGeneration" placeholder="请输入累计发电量，单位kWh 千瓦时" />
                </el-form-item>
                <el-form-item label="累计运行时间，单位h 小时" prop="totalRuntime">
                    <el-input v-model="form.totalRuntime" placeholder="请输入累计运行时间，单位h 小时" />
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listInverter, getInverter, delInverter, addInverter, updateInverter } from "@/api/business/inverter"
import { selectPvStationDataToSelect } from "@/api/business/pvStation"

export default {
    name: "Inverter",
    dicts: ['pv_station_device_type', 'pv_station_equipment_manufacturer'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 光伏电站逆变器设备信息表格数据
            inverterList: [],
            // 所属电站列表
            pvStationList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deviceName: null,
                deviceSn: null,
                devicePn: null,
                deviceCode: null,
                deviceType: null,
                pvId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                pvId: [
                    { required: true, message: "所属电站ID不能为空", trigger: "blur" }
                ],
                deviceName: [
                    { required: true, message: "设备名称不能为空", trigger: "blur" }
                ],
                deviceSn: [
                    { required: true, message: "设备SN不能为空", trigger: "blur" }
                ],
                devicePn: [
                    { required: true, message: "设备PN号不能为空", trigger: "blur" }
                ],
                manufacturer: [
                    { required: true, message: "设备厂商不能为空", trigger: "blur" }
                ],
                deviceCode: [
                    { required: true, message: "设备编码不能为空", trigger: "blur" }
                ],
                deviceModel: [
                    { required: true, message: "设备型号不能为空", trigger: "blur" }
                ],
                deviceType: [
                    { required: true, message: "设备类型不能为空", trigger: "change" }
                ],
                protocolVersion: [
                    { required: true, message: "协议版本不能为空", trigger: "blur" }
                ],
                createBy: [
                    { required: true, message: "创建人不能为空", trigger: "blur" }
                ],
                createTime: [
                    { required: true, message: "创建时间不能为空", trigger: "blur" }
                ]
            }
        }
    },
    created() {
        this.getList();
        this.getPvStationList();
    },
    methods: {
        /** 查询光伏电站逆变器设备信息列表 */
        getList() {
            this.loading = true
            listInverter(this.queryParams).then(response => {
                this.inverterList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 获取所属电站信息列表
        getPvStationList() {
            selectPvStationDataToSelect().then(response => {
                if (response.code == 200) {
                    this.pvStationList = response.data;
                }
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                deviceId: null,
                pvId: null,
                deviceName: null,
                deviceSn: null,
                devicePn: null,
                manufacturer: null,
                deviceCode: null,
                deviceModel: null,
                deviceType: null,
                protocolVersion: null,
                realtimePower: null,
                ratedPower: null,
                ratedVoltage: null,
                accessCapacity: null,
                fullLoadHours: null,
                dayPowerGeneration: null,
                monthPowerGeneration: null,
                yearPowerGeneration: null,
                totalPowerGeneration: null,
                totalRuntime: null,
                dcacSoftwareVersion: null,
                dcdcSoftwareVersion: null,
                dcacParamVersion: null,
                dcdcParamVersion: null,
                updateBy: null,
                updateTime: null,
                createBy: null,
                createTime: null
            }
            this.resetForm("form")
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.deviceId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加光伏电站逆变器设备信息"
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const deviceId = row.deviceId || this.ids
            getInverter(deviceId).then(response => {
                this.form = response.data
                this.open = true
                this.title = "修改光伏电站逆变器设备信息"
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.deviceId != null) {
                        updateInverter(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addInverter(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const deviceIds = row.deviceId || this.ids
            this.$modal.confirm('是否确认删除光伏电站逆变器设备信息编号为"' + deviceIds + '"的数据项？').then(function () {
                return delInverter(deviceIds)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => { })
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/inverter/export', {
                ...this.queryParams
            }, `inverter_${new Date().getTime()}.xlsx`)
        }
    }
}
</script>
